<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分类代码" prop="classificationCode">
        <el-input
          v-model="queryParams.classificationCode"
          placeholder="请输入分类代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="型号" prop="model">-->
<!--        <el-input-->
<!--          v-model="queryParams.model"-->
<!--          placeholder="请输入型号"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="测量范围" prop="measuringRange">-->
<!--        <el-input-->
<!--          v-model="queryParams.measuringRange"-->
<!--          placeholder="请输入测量范围"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="准确度等级或不确定度" prop="accuracyLevel">-->
<!--        <el-input-->
<!--          v-model="queryParams.accuracyLevel"-->
<!--          placeholder="请输入准确度等级或不确定度"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="生产厂家" prop="manufacturer">-->
<!--        <el-input-->
<!--          v-model="queryParams.manufacturer"-->
<!--          placeholder="请输入生产厂家"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="出厂编号" prop="factoryNumber">-->
<!--        <el-input-->
<!--          v-model="queryParams.factoryNumber"-->
<!--          placeholder="请输入出厂编号"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="管理状态" prop="status">-->
<!--        <el-select v-model="queryParams.status" placeholder="请选择管理状态" clearable size="small">-->
<!--          <el-option-->
<!--            v-for="dict in statusOptions"-->
<!--            :key="dict.dictValue"-->
<!--            :label="dict.dictLabel"-->
<!--            :value="dict.dictValue"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="确认间隔" prop="confirmInterval">-->
<!--        <el-input-->
<!--          v-model="queryParams.confirmInterval"-->
<!--          placeholder="请输入确认间隔"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="检定日期" prop="verificationDate">-->
<!--        <el-date-picker clearable size="small"-->
<!--          v-model="queryParams.verificationDate"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="选择检定日期">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="有效日期" prop="validityPeriod">-->
<!--        <el-date-picker clearable size="small"-->
<!--          v-model="queryParams.validityPeriod"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="选择有效日期">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="检定单位" prop="verificationUnit">-->
<!--        <el-input-->
<!--          v-model="queryParams.verificationUnit"-->
<!--          placeholder="请输入检定单位"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="使用部门" prop="departmentOfUse">-->
<!--        <el-input-->
<!--          v-model="queryParams.departmentOfUse"-->
<!--          placeholder="请输入使用部门"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="使用地点" prop="locationOfUse">-->
<!--        <el-input-->
<!--          v-model="queryParams.locationOfUse"-->
<!--          placeholder="请输入使用地点"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="ABC类" prop="abcCategory">-->
<!--        <el-input-->
<!--          v-model="queryParams.abcCategory"-->
<!--          placeholder="请输入ABC类"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用" prop="remarkSpare">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare"-->
<!--          placeholder="请输入备用"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用1" prop="remarkSpare1">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare1"-->
<!--          placeholder="请输入备用1"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用2" prop="remarkSpare2">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare2"-->
<!--          placeholder="请输入备用2"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ledger:instrument:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ledger:instrument:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ledger:instrument:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['ledger:instrument:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="instrumentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="分类代码" align="center" prop="classificationCode" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="型号" align="center" prop="model" />
      <el-table-column label="测量范围" align="center" prop="measuringRange" />
      <el-table-column label="准确度等级或不确定度" align="center" prop="accuracyLevel" />
      <el-table-column label="生产厂家" align="center" prop="manufacturer" />
      <el-table-column label="出厂编号" align="center" prop="factoryNumber" />
      <el-table-column label="管理状态" align="center" prop="status" />
      <el-table-column label="确认间隔" align="center" prop="confirmInterval" />
      <el-table-column label="检定日期" align="center" prop="verificationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.verificationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效日期" align="center" prop="validityPeriod" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.validityPeriod, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检定单位" align="center" prop="verificationUnit" />
      <el-table-column label="使用部门" align="center" prop="departmentOfUse" />
      <el-table-column label="使用地点" align="center" prop="locationOfUse" />
      <el-table-column label="ABC类" align="center" prop="abcCategory" />
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
<!--      <el-table-column label="备用" align="center" prop="remarkSpare" />-->
<!--      <el-table-column label="备用1" align="center" prop="remarkSpare1" />-->
<!--      <el-table-column label="备用2" align="center" prop="remarkSpare2" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ledger:instrument:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ledger:instrument:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改计量器具台账对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="170px">
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
        <el-form-item label="分类代码" prop="classificationCode">
          <el-input v-model="form.classificationCode" placeholder="请输入分类代码" />
        </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
        <el-form-item label="型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入型号" />
        </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
        <el-form-item label="测量范围" prop="measuringRange">
          <el-input v-model="form.measuringRange" placeholder="请输入测量范围" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
        <el-form-item label="准确度等级或不确定度" prop="accuracyLevel">
          <el-input v-model="form.accuracyLevel" placeholder="请输入准确度等级或不确定度" />
        </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
        <el-form-item label="生产厂家" prop="manufacturer">
          <el-input v-model="form.manufacturer" placeholder="请输入生产厂家" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
        <el-form-item label="出厂编号" prop="factoryNumber">
          <el-input v-model="form.factoryNumber" placeholder="请输入出厂编号" />
        </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
        <el-form-item label="管理状态" prop="status">
          <el-input v-model="form.status" placeholder="请输入管理状态" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
        <el-form-item label="确认间隔" prop="confirmInterval">
          <el-input v-model="form.confirmInterval" placeholder="请输入确认间隔" />
        </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
            <el-form-item label="检定单位" prop="verificationUnit">
              <el-input v-model="form.verificationUnit" placeholder="请输入检定单位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
        <el-form-item label="检定日期" prop="verificationDate">
          <el-date-picker clearable size="small"
            v-model="form.verificationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择检定日期"  style="width: 100%">
          </el-date-picker>
        </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
        <el-form-item label="有效日期" prop="validityPeriod">
          <el-date-picker clearable size="small"
            v-model="form.validityPeriod"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择有效日期"  style="width: 100%">
          </el-date-picker>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
        <el-form-item label="使用部门" prop="departmentOfUse">
<!--          <el-input v-model="form.departmentOfUse" placeholder="请输入使用部门" />-->
          <treeselect v-model="form.departmentOfUse" :options="deptOptions" :show-count="true" placeholder="请选择使用部门"  style="width: 100%"/>
        </el-form-item>
        </el-col>
        <el-col :span="12" :xs="24">
        <el-form-item label="使用地点" prop="locationOfUse">
          <el-input v-model="form.locationOfUse" placeholder="请输入使用地点" />
        </el-form-item>
        </el-col>
        </el-row>
        <el-form-item label="ABC类" prop="abcCategory">
          <el-input v-model="form.abcCategory" placeholder="请输入ABC类" />
        </el-form-item>
<!--        <el-form-item label="备注" prop="remark">-->
<!--          <el-input v-model="form.remark" placeholder="请输入备注" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用" prop="remarkSpare">-->
<!--          <el-input v-model="form.remarkSpare" placeholder="请输入备用" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用1" prop="remarkSpare1">-->
<!--          <el-input v-model="form.remarkSpare1" placeholder="请输入备用1" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用2" prop="remarkSpare2">-->
<!--          <el-input v-model="form.remarkSpare2" placeholder="请输入备用2" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInstrument, getInstrument, delInstrument, addInstrument, updateInstrument, exportInstrument } from "@/api/ledger/instrument";
import Treeselect from '@riophae/vue-treeselect'
import { treeselect } from "@/api/system/dept";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Instrument",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 计量器具台账表格数据
      instrumentList: [],
      // 部门树选项
      deptOptions: undefined,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        classificationCode: null,
        name: null,
        model: null,
        measuringRange: null,
        accuracyLevel: null,
        manufacturer: null,
        factoryNumber: null,
        status: null,
        confirmInterval: null,
        verificationDate: null,
        validityPeriod: null,
        verificationUnit: null,
        departmentOfUse: null,
        locationOfUse: null,
        abcCategory: null,
        remarkSpare: null,
        remarkSpare1: null,
        remarkSpare2: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        classificationCode: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "分类代码不能为空", trigger: "blur" }
        ],
        model: [
          { required: true, message: "型号不能为空", trigger: "blur" }
        ],
        measuringRange: [
          { required: true, message: "测量范围不能为空", trigger: "blur" }
        ],
        accuracyLevel: [
          { required: true, message: "准确度等级或不确定度不能为空", trigger: "blur" }
        ],
        manufacturer: [
          { required: true, message: "生产厂家不能为空", trigger: "blur" }
        ],
        factoryNumber: [
          { required: true, message: "出厂编号不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "管理状态不能为空", trigger: "blur" }
        ],
        confirmInterval: [
          { required: true, message: "确认间隔不能为空", trigger: "blur" }
        ],
        verificationDate: [
          { required: true, message: "检定日期不能为空", trigger: "blur" }
        ],
        validityPeriod: [
          { required: true, message: "有效日期不能为空", trigger: "blur" }
        ],
        verificationUnit: [
          { required: true, message: "检定单位不能为空", trigger: "blur" }
        ],
        departmentOfUse: [
          { required: true, message: "使用部门不能为空", trigger: "blur" }
        ],
        abcCategory: [
          { required: true, message: "ABC类不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    /** 查询计量器具台账列表 */
    getList() {
      this.loading = true;
      listInstrument(this.queryParams).then(response => {
        this.instrumentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
        // console.log('this.deptOptions:' + JSON.stringify(this.deptOptions))
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        classificationCode: null,
        name: null,
        model: null,
        measuringRange: null,
        accuracyLevel: null,
        manufacturer: null,
        factoryNumber: null,
        status: null,
        confirmInterval: null,
        verificationDate: null,
        validityPeriod: null,
        verificationUnit: null,
        departmentOfUse: null,
        locationOfUse: null,
        abcCategory: null,
        remark: null,
        remarkSpare: null,
        remarkSpare1: null,
        remarkSpare2: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加计量器具台账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInstrument(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改计量器具台账";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInstrument(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInstrument(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除计量器具台账编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInstrument(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有计量器具台账数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          // return exportInstrument(queryParams);
        this.download('/ledger/instrument/export', {
          ...this.queryParams
        }, `计量器具台账.xlsx`)
        this.exportLoading = false;
        }).then(response => {
          // this.download(response.msg);
          // this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
